home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d966.lha / UChessSrc / UChessSrc.lha / dspcom.c < prev    next >
C/C++ Source or Header  |  1994-02-11  |  41KB  |  1,789 lines

  1. /*
  2.  * dspcom.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include "gnuchess.h"
  24. extern INTSIZE Mwpawn[64], Mbpawn[64], Mknight[2][64], Mbishop[2][64];
  25. extern char *version, *patchlevel;
  26. char __aligned mvstr[4][6];
  27. char __aligned *InPtr;
  28. #define BOOKMENUNUM 0xc2
  29.  
  30. extern unsigned int TTadd;
  31.  
  32. extern int IllegalMove;
  33. int __aligned func_num=0;
  34. int __aligned thinkahead=0;
  35. int __aligned ThinkInARow=0;
  36. int __aligned ThinkAheadWorked=0;
  37. int __aligned ThinkAheadDepth=0;
  38.  
  39. extern int backsrchaborted;
  40. extern short int ISZERO;
  41. extern short __aligned background;
  42. int __aligned verifyquiet=0;
  43. int __aligned MouseDropped=0;
  44.  
  45. #include <ctype.h>
  46. #include <signal.h>
  47.  
  48. #ifdef AMIGA
  49. #define __USE_SYSBASE
  50. #include <exec/types.h>
  51. #include <exec/exec.h>
  52. #include <proto/exec.h>
  53. #include <proto/dos.h>
  54. #include <proto/graphics.h>
  55. #include <proto/intuition.h>
  56. struct IntuiMessage __aligned globalmessage;
  57. int __aligned globalmessage_valid=0;
  58. extern struct Window __aligned *wG;
  59. extern int procpri;
  60. extern struct Process *myproc;
  61. extern struct MenuItem MenuItem6;
  62. extern struct Menu Menu1;
  63. extern unsigned char __far cookedchar[128];
  64. extern struct Menu __aligned Menu1;
  65. #define MenuList1 Menu1
  66. extern struct MenuItem __aligned MenuItem8ab;
  67. extern int __aligned MenuStripSet;
  68. #endif
  69.  
  70. #define SIGQUIT SIGINT
  71.  
  72. #ifdef MSDOS
  73. #include <dos.h>
  74. #include <conio.h>
  75. #include <stdlib.h>
  76. #include <string.h>
  77. #include <time.h>
  78. #else
  79. #include <dos.h>
  80. #include <stdlib.h>
  81. #include <string.h>
  82. #include <time.h>
  83. /*
  84. #include <sys/param.h>
  85. #include <sys/types.h>
  86. #include <sys/file.h>
  87. #include <sys/ioctl.h>
  88. */
  89. #endif
  90.  
  91.  
  92. /*
  93.  * ataks.h - Header source for GNU CHESS
  94.  *
  95.  * Copyright (c) 1988,1989,1990 John Stanback
  96.  * Copyright (c) 1992 Free Software Foundation
  97.  *
  98.  * This file is part of GNU CHESS.
  99.  *
  100.  * GNU Chess is free software; you can redistribute it and/or modify
  101.  * it under the terms of the GNU General Public License as published by
  102.  * the Free Software Foundation; either version 2, or (at your option)
  103.  * any later version.
  104.  *
  105.  * GNU Chess is distributed in the hope that it will be useful,
  106.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  107.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  108.  * GNU General Public License for more details.
  109.  *
  110.  * You should have received a copy of the GNU General Public License
  111.  * along with GNU Chess; see the file COPYING.  If not, write to
  112.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  113.  */
  114. inline int
  115. SqAtakd2 (ARGSZ int sq, ARGSZ int side)
  116.  
  117. /*
  118.  * See if any piece with color 'side' ataks sq.  First check pawns then
  119.  * Queen, Bishop, Rook and King and last Knight.
  120.  */
  121.  
  122. {
  123.   register INTSIZE u;
  124.   register unsigned char *ppos, *pdir;
  125.   INTSIZE xside;
  126.  
  127.   xside = side ^ 1;
  128.   pdir = nextdir[ptype[xside][pawn]][sq];
  129.   u = pdir[sq];            /* follow captures thread */
  130.   if (u != sq)
  131.     {
  132.       if (board[u] == pawn && color[u] == side)
  133.     return (true);
  134.       u = pdir[u];
  135.       if (u != sq && board[u] == pawn && color[u] == side)
  136.     return (true);
  137.     }
  138.   /* king capture */
  139.   if (distance (sq, PieceList[side][0]) == 1)
  140.     return (true);
  141.   /* try a queen bishop capture */
  142.   ppos = nextpos[bishop][sq];
  143.   pdir = nextdir[bishop][sq];
  144.   u = ppos[sq];
  145.   do
  146.     {
  147.       if (color[u] == neutral)
  148.     u = ppos[u];
  149.       else
  150.     {
  151.       if (color[u] == side && (board[u] == queen || board[u] == bishop))
  152.         return (true);
  153.       u = pdir[u];
  154.     }
  155.   } while (u != sq);
  156.   /* try a queen rook capture */
  157.   ppos = nextpos[rook][sq];
  158.   pdir = nextdir[rook][sq];
  159.   u = ppos[sq];
  160.   do
  161.     {
  162.       if (color[u] == neutral)
  163.     u = ppos[u];
  164.       else
  165.     {
  166.       if (color[u] == side && (board[u] == queen || board[u] == rook))
  167.         return (true);
  168.       u = pdir[u];
  169.     }
  170.   } while (u != sq);
  171.   /* try a knight capture */
  172.   pdir = nextdir[knight][sq];
  173.   u = pdir[sq];
  174.   do
  175.     {
  176.       if (color[u] == side && board[u] == knight)
  177.     return (true);
  178.       u = pdir[u];
  179.   } while (u != sq);
  180.   return (false);
  181. }
  182.  
  183.  
  184. void
  185. algbr (INTSIZE int f, INTSIZE int t, INTSIZE int flag)
  186.  
  187.  
  188. /*
  189.  * Generate move strings in different formats.
  190.  */
  191.  
  192. {
  193.   int m3p;
  194.  
  195.   if (f != t)
  196.     {
  197.       /* algebraic notation */
  198.       mvstr[0][0] = cxx[column (f)];
  199.       mvstr[0][1] = rxx[row (f)];
  200.       mvstr[0][2] = cxx[column (t)];
  201.       mvstr[0][3] = rxx[row (t)];
  202.       mvstr[0][4] = mvstr[3][0] = '\0';
  203.       if (((mvstr[1][0] = pxx[board[f]]) == 'P') || (flag & promote))
  204.     {
  205.       if (mvstr[0][0] == mvstr[0][2])    /* pawn did not eat */
  206.         {
  207.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  208.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  209.           m3p = 2;
  210.         }
  211.       else
  212.         /* pawn ate */
  213.         {
  214.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* column */
  215.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  216.           mvstr[2][2] = mvstr[0][3];
  217.           m3p = 3;        /* to row */
  218.         }
  219.       if (flag & promote)
  220.         {
  221.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = qxx[flag & pmask];
  222.           mvstr[0][5] = mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[3][0] = '\0';
  223. #ifdef CHESSTOOL
  224.           mvstr[3][0] = mvstr[0][0];    /* Allow e7e8 for chesstool */
  225.           mvstr[3][1] = mvstr[0][1];
  226.           mvstr[3][2] = mvstr[0][2];
  227.           mvstr[3][3] = mvstr[0][3];
  228.           mvstr[3][4] = '\0';
  229. #endif
  230.         }
  231.       mvstr[2][m3p] = mvstr[1][2] = '\0';
  232.     }
  233.       else
  234.     /* not a pawn */
  235.     {
  236.       mvstr[2][0] = mvstr[1][0];
  237.       mvstr[2][1] = mvstr[0][1];
  238.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  239.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  240.       mvstr[2][4] = mvstr[1][3] = '\0';
  241.       strcpy (mvstr[3], mvstr[2]);
  242.       mvstr[3][1] = mvstr[0][0];
  243.       if (flag & cstlmask)
  244.         {
  245.           if (t > f)
  246.         {
  247.           strcpy (mvstr[1], CP[5]);
  248.           strcpy (mvstr[2], CP[7]);
  249.         }
  250.           else
  251.         {
  252.           strcpy (mvstr[1], CP[6]);
  253.           strcpy (mvstr[2], CP[8]);
  254.         }
  255.         }
  256.     }
  257.     }
  258.   else
  259.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  260. }
  261.  
  262.  
  263. int
  264. VerifyMove (char *s, INTSIZE int iop, INTSIZE unsigned int *mv)
  265.  
  266. /*
  267.  * Compare the string 's' to the list of legal moves available for the
  268.  * opponent. If a match is found, make the move on the board.
  269.  */
  270.  
  271. {
  272.   static INTSIZE pnt, tempb, tempc, tempsf, tempst, cnt;
  273.   int r,c,l;
  274.   char mystr[80];
  275.   char piece;
  276.   static struct leaf xnode;
  277.   struct leaf *node;
  278.  
  279.   if ((s[1] >= 'a') && (s[1] <= 'h'))
  280.    {
  281.     if ((s[0] == 'n') || (s[0] == 'p') || (s[0] == 'b') ||
  282.         (s[0] == 'k') || (s[0] == 'r') || (s[0] == 'q'))
  283.      {
  284.       s[0] = toupper(s[0]);
  285.      }
  286.    }
  287.   *mv = 0;
  288.   if (iop == 2)
  289.     {
  290.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  291.       return (false);
  292.     }
  293.   cnt = 0;
  294.   MoveList (opponent, 2);
  295.   pnt = TrPnt[2];
  296.   while (pnt < TrPnt[3])
  297.     {
  298.       node = &Tree[pnt++];
  299.       algbr (node->f, node->t, (INTSIZE) node->flags);
  300.       if (strcmp (s, mvstr[0]) == 0 || strcmp (s, mvstr[1]) == 0 ||
  301.       strcmp (s, mvstr[2]) == 0 || strcmp (s, mvstr[3]) == 0)
  302.     {
  303.       cnt++;
  304.       xnode = *node;
  305.     }
  306.     }
  307.   if (cnt == 1)
  308.     {
  309.       MakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  310.       if (SqAtakd2 (PieceList[opponent][0], computer))
  311.     {
  312.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  313. #if defined CHESSTOOL
  314.       printz (CP[15]);
  315. #else
  316. #ifdef NONDSP
  317. /* Illegal move in check */
  318. #ifndef AMIGA
  319.       printz (CP[77]);
  320.       printz ("\n");
  321. #else
  322.           DisplayComputerMove(CP[77]);
  323. #endif
  324. #else
  325. /* Illegal move in check */
  326.           if (!verifyquiet)
  327.         ShowMessage (CP[77]);
  328. #endif
  329. #endif /* CHESSTOOL */
  330.       return (false);
  331.     }
  332.       else
  333.     {
  334.       if (iop == 1)
  335.         return (true);
  336.       UpdateDisplay (xnode.f, xnode.t, 0, (INTSIZE) xnode.flags);
  337.       if ((board[xnode.t] == pawn)
  338.           || (xnode.flags & capture)
  339.           || (xnode.flags & cstlmask))
  340.         {
  341.           Game50 = GameCnt;
  342.           ZeroRPT ();
  343.         }
  344.       GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  345.       GameList[GameCnt].nodes = 0;
  346.       ElapsedTime (1);
  347.       GameList[GameCnt].time = (INTSIZE) et;
  348.       if (TCflag)
  349.         {
  350.           TimeControl.clock[opponent] -= et;
  351.           timeopp[oppptr] = et;
  352.           --TimeControl.moves[opponent];
  353.         }
  354.       *mv = (xnode.f << 8) | xnode.t;
  355.       algbr (xnode.f, xnode.t, false);
  356. #ifdef AMIGA
  357.       strcpy(mystr,mvstr[0]);
  358.       r = mystr[3] - '1';
  359.       c = mystr[2] - 'a';
  360.       l = ((flag.reverse) ? locn (7 - r, 7 - c) : locn (r, c));
  361.       if (color[l] == neutral)
  362.        {
  363.     DisplayBeep(0L);
  364.         Delay(25L);
  365.     DisplayBeep(0L);
  366.         Delay(25L);
  367.     DisplayBeep(0L);
  368.         Delay(25L);
  369.         piece = ' ';
  370.        }
  371.       else if (color[l] == white)
  372.         piece = qxx[board[l]]; /* white are lower case pieces */
  373.       else
  374.         piece = pxx[board[l]]; /* black are upper case pieces */
  375.       AnimateAmigaMove(mystr,piece);
  376. #endif
  377.       return (true);
  378.     }
  379.     }
  380. #if defined CHESSTOOL
  381.   printz (CP[78]);
  382. #else
  383. #ifdef NONDSP
  384. /* Illegal move */
  385. #ifdef AMIGA
  386.   sprintf(mystr,CP[75],s);
  387.   if (!verifyquiet)
  388.    DisplayComputerMove(mystr);
  389. #else
  390.   printz (CP[75], s);
  391. #endif
  392. #ifdef DEBUG8
  393.   if (1)
  394.     {
  395.       FILE *D;
  396.       int r, c, l;
  397.       extern unsigned INTSIZE int PrVar[];
  398.       D = fopen ("/tmp/DEBUG", "a+");
  399.       pnt = TrPnt[2];
  400.       fprintf (D, "resp = %d\n", ResponseTime);
  401.       fprintf (D, "iop = %d\n", iop);
  402.       fprintf (D, "matches = %d\n", cnt);
  403.       algbr (hint >> 8, hint & 0xff, (INTSIZE) 0);
  404.       fprintf (D, "hint %s\n", mvstr[0]);
  405.       fprintf (D, "inout move is %s\n", s);
  406.       for (r = 1; PrVar[r]; r++)
  407.     {
  408.       algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (INTSIZE) 0);
  409.       fprintf (D, " %s", mvstr[0]);
  410.     }
  411.       fprintf (D, "\n");
  412.       fprintf (D, "legal move are \n");
  413.       while (pnt < TrPnt[3])
  414.     {
  415.       node = &Tree[pnt++];
  416.       algbr (node->f, node->t, (INTSIZE) node->flags);
  417.       fprintf (D, "%s %s %s %s\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3]);
  418.     }
  419.       fprintf (D, "\n current board is\n");
  420.       for (r = 7; r >= 0; r--)
  421.     {
  422.       for (c = 0; c <= 7; c++)
  423.         {
  424.           l = locn (r, c);
  425.           if (color[l] == neutral)
  426.         fprintf (D, " -");
  427.           else if (color[l] == white)
  428.         fprintf (D, " %c", qxx[board[l]]);
  429.           else
  430.         fprintf (D, " %c", pxx[board[l]]);
  431.         }
  432.       fprintf (D, "\n");
  433.     }
  434.       fprintf (D, "\n");
  435.       fclose (D);
  436.       abort ();
  437.     }
  438. #endif
  439. #else
  440. /* Illegal move */
  441.  if (!verifyquiet)
  442.   ShowMessage (CP[76]);
  443. #endif
  444. #endif /* CHESSTOOL */
  445. #if !defined CHESSTOOL && !defined XBOARD
  446.   if (cnt > 1)
  447.     ShowMessage (CP[32]);
  448. #endif /* CHESSTOOL */
  449.   return (false);
  450. }
  451.  
  452. int
  453. parser (char *f, int side)
  454. {
  455.   int c1, r1, c2, r2;
  456.  
  457.   if (f[4] == 'o')
  458.     if (side == black)
  459.       return 0x3C3A;
  460.     else
  461.       return 0x0402;
  462.   else if (f[0] == 'o')
  463.     if (side == black)
  464.       return 0x3C3E;
  465.     else
  466.       return 0x0406;
  467.   else
  468.     {
  469.       c1 = f[0] - 'a';
  470.       r1 = f[1] - '1';
  471.       c2 = f[2] - 'a';
  472.       r2 = f[3] - '1';
  473.       return (locn (r1, c1) << 8) | locn (r2, c2);
  474.     }
  475.   /*NOTREACHED*/
  476. }
  477.  
  478. int myfgets(char *buff,int len,BPTR fd)
  479. {
  480.  char tmpch;
  481.  int done=0;
  482.  int numchars=0;
  483.  int retval;
  484.  
  485.  retval = 1;
  486.  do{
  487.     if (Read(fd,&tmpch,1L) != 1L)
  488.      {
  489.       done = 1;
  490.       retval = 0;
  491.      }
  492.     else
  493.      {
  494.       buff[numchars++] = tmpch;
  495.       if (tmpch == '\n')
  496.        {
  497.         done = 1;
  498.        }
  499.       if (numchars >= (len-1))
  500.        {
  501.         done = 1;
  502.         retval = 0;
  503.        }
  504.      }
  505.   } while (!done);
  506.  buff[numchars] = '\0';
  507.  return(retval);
  508. }
  509.  
  510. void
  511. GetGame (void)
  512. {
  513.   char checkstr[8];
  514.   BPTR fd;
  515.   char fname[256], *p;
  516.   int c, i, j;
  517.   INTSIZE sq;
  518.  
  519. /* enter file name */
  520.  if (!GetFileName(fname))
  521.   {
  522.    return;
  523.   }
  524.  (void)SetTaskPri((struct Task *)myproc,0);
  525.  if (fname[0] == 0)
  526.   ShowMessage (CP[63]);
  527. #ifndef AMIGA
  528.   scanz ("%s", fname);
  529. #endif
  530. /* chess.000 */
  531.   if (fname[0] == '\0')
  532.     strcpy (fname, CP[137]);
  533.   ShowMessage("Loading Game..");
  534.   Delay(20L);
  535.   if ((fd = Open (fname, MODE_OLDFILE)) != NULL)
  536.     {
  537.       Delay(20L);
  538.       NewGame ();
  539.       ShowMessage("Loading Game..");
  540.       Delay(10L);
  541.       myfgets (fname, 256, fd);
  542.       for(i=0;i<5;i++)
  543.        checkstr[i] = fname[i];
  544.       checkstr[5] = 0;
  545.       if (stricmp(checkstr,"Black"))
  546.        {
  547.         DisplayBeep(0L);
  548.         Delay(25L);
  549.         DisplayBeep(0L);
  550.         Close(fd);
  551.         return;
  552.        }
  553.       computer = opponent = white;
  554.       InPtr = fname;
  555.       skip ();
  556.       if (*InPtr == 'c')
  557.     computer = black;
  558.       else
  559.     opponent = black;
  560.       skip ();
  561.       skip ();
  562.       skip ();
  563.       Game50 = atoi (InPtr);
  564.       myfgets (fname, 256, fd);
  565.       InPtr = &fname[14];
  566.       castld[white] = ((*InPtr == CP[214][0]) ? true : false);
  567.       skip ();
  568.       skip ();
  569.       castld[black] = ((*InPtr == CP[214][0]) ? true : false);
  570.       myfgets (fname, 256, fd);
  571.       InPtr = &fname[11];
  572.       skipb ();
  573.       TCflag = atoi (InPtr);
  574.       skip ();
  575.       InPtr += 14;
  576.       skipb ();
  577.       OperatorTime = atoi (InPtr);
  578.       myfgets (fname, 256, fd);
  579.       InPtr = &fname[11];
  580.       skipb ();
  581.       TimeControl.clock[white] = atoi (InPtr);
  582.       skip ();
  583.       skip ();
  584.       TimeControl.moves[white] = atoi (InPtr);
  585.       myfgets (fname, 256, fd);
  586.       InPtr = &fname[11];
  587.       skipb ();
  588.       TimeControl.clock[black] = atoi (InPtr);
  589.       skip ();
  590.       skip ();
  591.       TimeControl.moves[black] = atoi (InPtr);
  592.       myfgets (fname, 256, fd);
  593.       for (i = 7; i > -1; i--)
  594.     {
  595.       myfgets (fname, 256, fd);
  596.       p = &fname[2];
  597.       InPtr = &fname[11];
  598.       skipb ();
  599.       for (j = 0; j < 8; j++)
  600.         {
  601.           sq = i * 8 + j;
  602.           if (*p == '.')
  603.         {
  604.           board[sq] = no_piece;
  605.           color[sq] = neutral;
  606.         }
  607.           else
  608.         {
  609.           for (c = 0; c < 8; c++)
  610.             {
  611.               if (*p == pxx[c])
  612.             {
  613.               board[sq] = c;
  614.               color[sq] = black;
  615.             }
  616.             }
  617.           for (c = 0; c < 8; c++)
  618.             {
  619.               if (*p == qxx[c])
  620.             {
  621.               board[sq] = c;
  622.               color[sq] = white;
  623.             }
  624.             }
  625.         }
  626.           p++;
  627.           Mvboard[sq] = atoi (InPtr);
  628.           skip ();
  629.         }
  630.     }
  631.       GameCnt = 0;
  632.       flag.regularstart = false/*true*/;
  633.       myfgets (fname, 256, fd);
  634.       myfgets (fname, 256, fd);
  635.       myfgets (fname, 256, fd);
  636.       while (myfgets (fname, 256, fd))
  637.     {
  638.       struct GameRec *g;
  639.       int side = black; // was side = computer in 2.51
  640.  
  641. /*printf("in while loop\n");*/
  642.       side = side ^ 1;
  643.       ++GameCnt;
  644.       InPtr = fname;
  645.       skipb ();
  646.       g = &GameList[GameCnt];
  647.       g->gmove = parser (InPtr, side);
  648.       skip ();
  649.       g->score = atoi (InPtr);
  650.       skip ();
  651.       g->depth = atoi (InPtr);
  652.       skip ();
  653.       g->nodes = atoi (InPtr);
  654.       skip ();
  655.       g->time = atoi (InPtr);
  656.       skip ();
  657.       g->flags = c = atoi (InPtr);
  658.       skip ();
  659.       g->hashkey = strtol (InPtr, (char **) NULL, 16);
  660.       skip ();
  661.       g->hashbd = strtol (InPtr, (char **) NULL, 16);
  662.       g->piece = no_piece;
  663.       g->color = neutral;
  664.       if (c & (capture | cstlmask))
  665.         {
  666.           if (c & capture)
  667.         {
  668.           skip ();
  669.           for (c = 0; c < 8; c++)
  670.             if (pxx[c] == *InPtr)
  671.               break;
  672.           g->piece = c;
  673.         }
  674.           skip ();
  675.           g->color = ((*InPtr == CP[119][0]) ? black : white);
  676.         }
  677.     }
  678.       /* GameCnt--; */
  679.       if (TimeControl.clock[white] > 0)
  680.     TCflag = true;
  681.       Close (fd);
  682.     }
  683.   ISZERO = 1;
  684.   ZeroRPT ();
  685.   InitializeStats ();
  686.   UpdateDisplay (0, 0, 1, 0);
  687.   Sdepth = 0;
  688.   hint = 0;
  689.   DisableMoveNow();
  690.   if (!TCflag)
  691.    {
  692.     EnableMoveNow();
  693.    }
  694.   else
  695.    {
  696.     i = player;
  697.     player = black;
  698.     UpdateClocks();
  699.     player = white;
  700.     UpdateClocks();
  701.     player = i;
  702.     if ((((TimeControl.clock[black])/TimeControl.moves[black]) > 5900) ||
  703.         (((TimeControl.clock[white])/TimeControl.moves[white]) > 5900))
  704.      {
  705.       EnableMoveNow();
  706.      }
  707.    }
  708.   flag.regularstart = false;
  709.   Book = 0;
  710.   if (MenuStripSet)
  711.    {
  712.     
  713.     MenuItem8ab.Flags &= (0xffff ^ CHECKED);
  714.     SetMenuStrip(wG,&MenuList1);    /* attach any Menu */
  715.    }
  716.   ShowMessage("Game Loaded");
  717. #ifdef AMIGA
  718.   DrawAmigaBoard();
  719. #endif
  720.   (void)SetTaskPri((struct Task *)myproc,procpri);
  721. }
  722.  
  723. void
  724. GetXGame (void)
  725. {
  726.   BPTR fd;
  727.   char fname[256], *p;
  728.   int c, i, j;
  729.   INTSIZE sq;
  730. /* Enter file name */
  731.   ShowMessage (CP[63]);
  732. #ifndef AMIGA
  733.   scanz ("%s", fname);
  734. #endif
  735.   if (fname[0] == '\0')
  736. /* xboard.position.read*/
  737.     strcpy (fname, CP[205]);
  738.   if ((fd = Open (fname, MODE_OLDFILE)) != NULL)
  739.     {
  740.       NewGame ();
  741.       flag.regularstart = false;
  742.       Book = false;
  743.       myfgets (fname, 256, fd);
  744.       fname[6] = '\0';
  745.       if (strcmp (fname, CP[206]))
  746.     return;
  747.       myfgets (fname, 256, fd);
  748.       myfgets (fname, 256, fd);
  749.       for (i = 7; i > -1; i--)
  750.     {
  751.       myfgets (fname, 256, fd);
  752.       p = fname;
  753.       for (j = 0; j < 8; j++)
  754.         {
  755.           sq = i * 8 + j;
  756.           if (*p == '.')
  757.         {
  758.           board[sq] = no_piece;
  759.           color[sq] = neutral;
  760.         }
  761.           else
  762.         {
  763.           for (c = 0; c < 8; c++)
  764.             {
  765.               if (*p == qxx[c])
  766.             {
  767.               board[sq] = c;
  768.               color[sq] = black;
  769.             }
  770.             }
  771.           for (c = 0; c < 8; c++)
  772.             {
  773.               if (*p == pxx[c])
  774.             {
  775.               board[sq] = c;
  776.               color[sq] = white;
  777.             }
  778.             }
  779.         }
  780.           p += 2;
  781.         }
  782.     }
  783.       Close (fd);
  784.     }
  785.   ISZERO = 1;
  786.   ZeroRPT ();
  787.   InitializeStats ();
  788.   UpdateDisplay (0, 0, 1, 0);
  789.   Sdepth = 0;
  790.   hint = 0;
  791. }
  792.  
  793. void
  794. SaveGame (void)
  795. {
  796.   FILE *fd;
  797.   char fname[256];
  798.   INTSIZE sq, i, c, f, t;
  799.   char p;
  800.  
  801.   if (!(GetFileName(savefile)))   
  802.    { 
  803.     return;
  804.    }
  805.   (void)SetTaskPri((struct Task *)myproc,0);
  806.   if (savefile[0])
  807.     strcpy (fname, savefile);
  808.   else
  809.     {
  810. /* Enter file name*/
  811.       ShowMessage (CP[63]);
  812. #ifndef AMIGA
  813.       scanz ("%s", fname);
  814. #endif
  815.     }
  816.  
  817.   if (fname[0] == '\0')
  818. /* chess.000 */
  819.     strcpy (fname, CP[137]);
  820.   Delay(25L);
  821.   if ((fd = fopen (fname, "w")) != NULL)
  822.     {
  823.       char *b, *w;
  824.  
  825.       Delay(25L);
  826.       b = w = CP[74];
  827.       if (computer == black)
  828.     b = CP[141];
  829.       if (computer == white)
  830.     w = CP[141];
  831.       fprintf (fd, CP[37], b, w, Game50);
  832.       fprintf (fd, CP[42], castld[white] ? CP[214] : CP[215], castld[black] ? CP[214] : CP[215]);
  833.       fprintf (fd, CP[111], TCflag, OperatorTime);
  834.       fprintf (fd, CP[117],
  835.            TimeControl.clock[white], TimeControl.moves[white],
  836.            TimeControl.clock[black], TimeControl.moves[black]);
  837.       for (i = 7; i > -1; i--)
  838.     {
  839.       fprintf (fd, "%1d ", i + 1);
  840.       for (c = 0; c < 8; c++)
  841.         {
  842.           sq = i * 8 + c;
  843.           switch (color[sq])
  844.         {
  845.         case black:
  846.           p = pxx[board[sq]];
  847.           break;
  848.         case white:
  849.           p = qxx[board[sq]];
  850.           break;
  851.         default:
  852.           p = '.';
  853.         }
  854.           fprintf (fd, "%c", p);
  855.         }
  856.       for (f = i * 8; f < i * 8 + 8; f++)
  857.         fprintf (fd, " %d", Mvboard[f]);
  858.       fprintf (fd, "\n");
  859.     }
  860.       fprintf (fd, "  %s\n", cxx);
  861.       fprintf (fd, CP[126]);
  862.       for (i = 1; i <= GameCnt; i++)
  863.     {
  864.       struct GameRec *g = &GameList[i];
  865.  
  866.       f = g->gmove >> 8;
  867.       t = (g->gmove & 0xFF);
  868.       algbr (f, t, g->flags);
  869.       fprintf (fd, "%s %5d %5d %7ld %5d %5d  %#08lx %#08lx  %c   %s\n",
  870.            mvstr[0], g->score, g->depth,
  871.            g->nodes, g->time, g->flags, g->hashkey, g->hashbd,
  872.        pxx[g->piece], ((g->color == 2) ? "     " : ColorStr[g->color]));
  873.     }
  874.       fclose (fd);
  875. /* Game saved */
  876.       ShowMessage (CP[70]);
  877.     }
  878.   else
  879.     /*ShowMessage ("Could not open file");*/
  880.     ShowMessage (CP[48]);
  881.   (void)SetTaskPri((struct Task *)myproc,procpri);
  882. }
  883.  
  884. void
  885. ListGame (getstr)
  886. int getstr;
  887. {
  888.   FILE *fd;
  889.   INTSIZE i, f, t;
  890.   long when;
  891.   char fname[256], dbuf[256];
  892.  
  893.   if (listfile[0])
  894.     strcpy (fname, listfile);
  895.   else
  896.     {
  897. #ifdef MSDOS
  898.       sprintf (fname, "chess.lst");
  899. #else
  900.       if (!getstr)
  901.        {
  902.         time (&when);
  903.         strncpy (dbuf, ctime (&when), 20);
  904.         dbuf[7] = '\0';
  905.         dbuf[10] = '\0';
  906.         dbuf[13] = '\0';
  907.         dbuf[16] = '\0';
  908.         dbuf[19] = '\0';
  909. /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
  910.    date is Jan 1
  911.    time is 02:03:04
  912.    program played black */
  913.         sprintf (fname, "t:UC%s.%s%s-%s%s%s%c", patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14, dbuf + 17, ColorStr[computer][0]);
  914.         /* replace space padding with 0 */
  915.         for (i = 0; fname[i] != '\0'; i++)
  916.         if (fname[i] == ' ')
  917.         fname[i] = '0';
  918.        }
  919.       else
  920.        {
  921.         if (!GetFileName(fname))
  922.          {
  923.           return;
  924.          }
  925.        }
  926. #endif /* MSDOS */
  927.     }
  928.   Delay(5L);
  929.   (void)SetTaskPri((struct Task *)myproc,0);
  930.   fd = fopen (fname, "w");
  931.   Delay(5L);
  932.   if (!fd)
  933.     {
  934.      (void)SetTaskPri((struct Task *)myproc,procpri);
  935.      return;
  936.     }
  937.   /*fprintf (fd, "gnuchess game %d\n", u);*/
  938.   fprintf (fd, "%s\n", VERSTRING);
  939.   fprintf (fd, CP[10]);
  940.   fprintf (fd, CP[11]);
  941.   for (i = 1; i <= GameCnt; i++)
  942.     {
  943.       f = GameList[i].gmove >> 8;
  944.       t = (GameList[i].gmove & 0xFF);
  945.       algbr (f, t, GameList[i].flags);
  946.       if(GameList[i].flags & book)
  947.           fprintf (fd, "%5s  %5d    Book%7ld %5d", mvstr[0],
  948.            GameList[i].score, 
  949.            GameList[i].nodes, GameList[i].time);
  950.       else
  951.           fprintf (fd, "%5s  %5d     %2d %7ld %5d", mvstr[0],
  952.            GameList[i].score, GameList[i].depth,
  953.            GameList[i].nodes, GameList[i].time);
  954.       if ((i % 2) == 0)
  955.     {
  956.       fprintf (fd, "\n");
  957. #ifdef DEBUG40
  958.       if (computer == black)
  959.         fprintf (fd, " %d %d %d %d %d %d %d\n",
  960.              GameList[i].d1,
  961.              GameList[i].d2,
  962.              GameList[i].d3,
  963.              GameList[i].d4,
  964.              GameList[i].d5,
  965.              GameList[i].d6,
  966.              GameList[i].d7);
  967.       else
  968.         fprintf (fd, " %d %d %d %d %d %d %d\n",
  969.              GameList[i - 1].d1,
  970.              GameList[i - 1].d2,
  971.              GameList[i - 1].d3,
  972.              GameList[i - 1].d4,
  973.              GameList[i - 1].d5,
  974.              GameList[i - 1].d6,
  975.              GameList[i - 1].d7);
  976. #endif
  977.     }
  978.       else
  979.     fprintf (fd, "         ");
  980.     }
  981.   fprintf (fd, "\n\n");
  982.   if (GameList[GameCnt].flags & draw)
  983.     {
  984.       fprintf (fd, CP[54], DRAW);
  985.     }
  986.   else if (GameList[GameCnt].score == -9999)
  987.     {
  988.       fprintf (fd, "%s\n", ColorStr[player ]);
  989.     }
  990.   else if (GameList[GameCnt].score == 9998)
  991.     {
  992.       fprintf (fd, "%s\n", ColorStr[player ^ 1]);
  993.     }
  994.   fclose (fd);
  995.   (void)SetTaskPri((struct Task *)myproc,procpri);
  996. }
  997.  
  998. void
  999. Undo (void)
  1000.  
  1001. /*
  1002.  * Undo the most recent half-move.
  1003.  */
  1004.  
  1005. {
  1006.   INTSIZE f, t;
  1007.   f = GameList[GameCnt].gmove >> 8;
  1008.   t = GameList[GameCnt].gmove & 0xFF;
  1009.   if (board[t] == king && distance (t, f) > 1)
  1010.     (void) castle (GameList[GameCnt].color, f, t, 2);
  1011.   else
  1012.     {
  1013.       /* Check for promotion: */
  1014.       if (GameList[GameCnt].flags & promote)
  1015.     {
  1016.       board[t] = pawn;
  1017.     }
  1018.       board[f] = board[t];
  1019.       color[f] = color[t];
  1020.       board[t] = GameList[GameCnt].piece;
  1021.       color[t] = GameList[GameCnt].color;
  1022.       if (color[t] != neutral)
  1023.     Mvboard[t]--;
  1024.       Mvboard[f]--;
  1025.     }
  1026.   if (GameList[GameCnt].flags & epmask)
  1027.     EnPassant (otherside[color[f]], f, t, 2);
  1028.   else
  1029.     InitializeStats ();
  1030.   epsquare = GameList[GameCnt].epssq;
  1031.   if (TCflag && (TCmoves>1))
  1032.     ++TimeControl.moves[color[f]];
  1033.   hashkey = GameList[GameCnt].hashkey;
  1034.   hashbd = GameList[GameCnt].hashbd;
  1035.   GameCnt--;
  1036.   computer = computer ^ 1;
  1037.   opponent = opponent ^ 1;
  1038.   Mate = flag.mate = false;
  1039.   Sdepth = 0;
  1040.   player = player ^ 1;
  1041.   ShowSidetoMove ();
  1042.   UpdateDisplay (0, 0, 1, 0);
  1043.  
  1044.   InitializeStats();
  1045.   ZeroRPT();
  1046.   ZeroTTable();
  1047. #ifdef HISTORY
  1048. #ifdef AMIGA
  1049.  ClearMem(history,sizeof(history));
  1050. #else
  1051.  memset(history,0,sizeof(history));
  1052. #endif
  1053. #endif
  1054.  
  1055. /*  if (flag.regularstart)
  1056.     Book = BOOKFAIL;*/
  1057. }
  1058.  
  1059. void
  1060.  TestSpeed (void (*f) ( INTSIZE int side, INTSIZE int ply), unsigned j)
  1061. {
  1062.   char astr[256];
  1063.   INTSIZE i;
  1064.   long cnt, rate, t1, t2;
  1065.  
  1066.   t1 = time (0);
  1067.   Forbid();
  1068.   for (i = 0; i < j; i++)
  1069.     {
  1070.       f (opponent, 2);
  1071.     }
  1072.   Permit();
  1073.   t2 = time (0);
  1074.   cnt = j * (TrPnt[3] - TrPnt[2]);
  1075.   if (t2 - t1)
  1076.     et = (t2 - t1) * 100;
  1077.   else
  1078.     et = 1;
  1079.   rate = (et) ? (cnt / et) : 0;
  1080.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  1081. #ifdef NONDSP
  1082. #ifdef AMIGA
  1083. if (!func_num)
  1084.  sprintf(astr,"Mlst=%dN/s",rate*100);
  1085. else if (func_num == 1)
  1086.  sprintf(astr,"Clst=%dN/s",rate*100);
  1087. ShowMessage(astr);
  1088. #else
  1089.   printz (CP[91], cnt, rate*100);
  1090. #endif
  1091. #ifdef DEBUG9
  1092.   for (j = TrPnt[2]; j < TrPnt[3]; j++)
  1093.     {
  1094.       struct leaf *node = &Tree[j];
  1095.       algbr (node->f, node->t, node->flags);
  1096.       printf ("%s %s %s %s %d %x\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],node->score,node->flags);
  1097.     }
  1098. #endif
  1099. #else
  1100.   ShowNodeCnt (cnt);
  1101. #endif
  1102. }
  1103.  
  1104. void
  1105.  TestPSpeed (INTSIZE int (*f) (INTSIZE int side), unsigned j)
  1106. {
  1107.   char astr[256];
  1108.   INTSIZE i;
  1109.   long cnt, rate, t1, t2;
  1110.  
  1111.   t1 = time (0);
  1112.   Forbid();
  1113.   for (i = 0; i < j; i++)
  1114.     {
  1115.       (void) f (opponent);
  1116.     }
  1117.   Permit();
  1118.   t2 = time (0);
  1119.   cnt = j;
  1120.   if (t2 - t1)
  1121.     et = (t2 - t1) * 100;
  1122.   else
  1123.     et = 1;
  1124.   rate = (et) ? (cnt / et) : 0;
  1125.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  1126. #ifdef NONDSP
  1127. #ifdef AMIGA
  1128. sprintf(astr,"Eval=%ldN/s",rate*100);
  1129. ShowMessage(astr);
  1130. #else
  1131.   printz (CP[91], cnt, rate*100);
  1132. #endif
  1133. #else
  1134.   ShowNodeCnt (cnt);
  1135. #endif
  1136. }
  1137.  
  1138.  
  1139. void
  1140. SetMachineTime (char *s)
  1141. {
  1142.   char *time;
  1143.   int m, t;
  1144.   time = &s[strlen (CP[197])];
  1145.   m = strtol (time, &time, 10);
  1146.   t = strtol (time, &time, 10);
  1147.   if (t)
  1148.     TimeControl.clock[computer] = t;
  1149.   if (m)
  1150.     TimeControl.moves[computer] = m;
  1151. #ifdef XBOARD
  1152.   printz (CP[222], m, t);
  1153. #endif
  1154. }
  1155.  
  1156.  
  1157. void
  1158. InputCommand (cstring)
  1159.  
  1160. char *cstring;
  1161.  
  1162. /*
  1163.  * Process the users command. If easy mode is OFF (the computer is thinking
  1164.  * on opponents time) and the program is out of book, then make the 'hint'
  1165.  * move on the board and call SelectMove() to find a response. The user
  1166.  * terminates the search by entering ^C (quit siqnal) before entering a
  1167.  * command. If the opponent does not make the hint move, then set Sdepth to
  1168.  * zero.
  1169.  */
  1170.  
  1171. {
  1172.   int need_to_zero;
  1173.   char tstr[40];
  1174.   int i = 0;
  1175.   INTSIZE have_shown_prompt = false;
  1176.   INTSIZE ok, tmp;
  1177.   unsigned INTSIZE mv;
  1178.   char s[80], sx[80];
  1179.  
  1180. #if defined CHESSTOOL
  1181.   INTSIZE normal = false;
  1182.  
  1183. #endif
  1184.  
  1185.   ok = flag.quit = false;
  1186.   player = opponent;
  1187.   ft = 0;
  1188. #ifdef CACHE
  1189.   if(TTadd > ttblsize) // want to zero out ttable each time
  1190.    ZeroTTable();
  1191. #endif
  1192.   if (hint > 0 && !flag.easy && !flag.force)
  1193.     if ((board[hint >> 8] != pawn) || ((row (hint & 0x3f) != 0) && (row (hint & 0x3f) != 7)))
  1194.       {
  1195.         thinkahead = 1;
  1196. /*    fflush (stdout);*/
  1197.         ft = time0;
  1198.     /*time0 = time ((long *) 0);*/
  1199.     algbr ((INTSIZE) hint >> 8, (INTSIZE) hint & 0x3F, false);
  1200.     strcpy (s, mvstr[0]);
  1201.     tmp = epsquare;
  1202. #ifdef DEBUG12
  1203.     if (1)
  1204.       {
  1205.         FILE *D;
  1206.         int r, c, l;
  1207.         extern unsigned INTSIZE int PrVar[];
  1208.         D = fopen ("/tmp/DEBUGA", "a+");
  1209.         fprintf (D, "score = %d\n", root->score);
  1210.         fprintf (D, "inout move is %s\n", s);
  1211.         for (r = 1; PrVar[r]; r++)
  1212.           {
  1213.         algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (INTSIZE) 0);
  1214.         fprintf (D, " %s", mvstr[0]);
  1215.           }
  1216.         fprintf (D, "\n current board is\n");
  1217.         for (r = 7; r >= 0; r--)
  1218.           {
  1219.         for (c = 0; c <= 7; c++)
  1220.           {
  1221.             l = locn (r, c);
  1222.             if (color[l] == neutral)
  1223.               fprintf (D, " -");
  1224.             else if (color[l] == white)
  1225.               fprintf (D, " %c", qxx[board[l]]);
  1226.             else
  1227.               fprintf (D, " %c", pxx[board[l]]);
  1228.           }
  1229.         fprintf (D, "\n");
  1230.           }
  1231.         fprintf (D, "\n");
  1232.         fclose (D);
  1233.       }
  1234. #endif
  1235. #if !defined CHESSTOOL
  1236. #ifndef AMIGA
  1237.     if (flag.post)
  1238.       GiveHint ();
  1239. #endif
  1240. #endif
  1241.         verifyquiet = 1;
  1242.     if (VerifyMove (s, 1, &mv))
  1243.       {
  1244.         Sdepth = 0;
  1245. #ifdef QUIETBACKGROUND
  1246. #ifdef NONDSP
  1247.         PromptForMove ();
  1248. #else
  1249.         ShowSidetoMove ();
  1250.         ShowPrompt ();
  1251. #endif
  1252.         have_shown_prompt = true;
  1253. #endif /* QUIETBACKGROUND */
  1254.             backsrchaborted = 0;
  1255.         SelectMove (computer, 2);
  1256. #ifdef OLD_LOOKAHEAD
  1257.         VerifyMove (s, 2, &mv);
  1258.         Sdepth = 0;
  1259. #else // faster lookahead on predict
  1260.         VerifyMove (s, 2, &mv);
  1261. //sprintf(tstr,"sd = %d",Sdepth);
  1262. //ShowMessage(tstr);
  1263.         if ((Sdepth > 0)&&(backsrchaborted))
  1264.               Sdepth--;
  1265. #endif
  1266.       }
  1267.          verifyquiet = 0;
  1268.     /*ft = (time ((long *) 0) - time0) * 100;*/
  1269.     epsquare = tmp;
  1270.     time0 = ft;
  1271.       }
  1272.   while (!(ok || flag.quit))
  1273.     {
  1274.       need_to_zero = 0;
  1275. #if defined CHESSTOOL
  1276.       normal = false;
  1277. #endif
  1278.       player = opponent;
  1279. #ifdef QUIETBACKGROUND
  1280.       if (!have_shown_prompt)
  1281.     {
  1282. #endif /* QUIETBACKGROUND */
  1283. #ifdef NONDSP
  1284.       PromptForMove ();
  1285. #else
  1286.       ShowSidetoMove ();
  1287.       ShowPrompt ();
  1288. #endif
  1289. #ifdef QUIETBACKGROUND
  1290.     }
  1291.       have_shown_prompt = false;
  1292. #endif /* QUIETBACKGROUND */
  1293. #ifdef NONDSP
  1294.       s[0] = sx[0] = '\0';
  1295. #ifndef AMIGA
  1296.       while (!sx[0])
  1297.     i = (int) gets (sx);
  1298. #else
  1299.        thinking2 = 0;
  1300.        i = 1;
  1301.        thinkahead = 0;
  1302.        GetOperatorEntry(sx);
  1303. #endif
  1304. #else
  1305.      /* fflush (stdout);
  1306.       i = (int) getstr (sx);*/
  1307. #endif
  1308.       sscanf (sx, "%s", s);
  1309.       if (i == EOF)
  1310.     ExitChess ();
  1311.       if (s[0] == '\0')
  1312.     continue;
  1313.       if (strcmp (s, CP[131]) == 0)    /*bd*/
  1314.     {
  1315. #if defined CHESSTOOL || defined XBOARD
  1316.       chesstool = 0;
  1317. #endif /* CHESSTOOL */
  1318.       ClrScreen ();
  1319.       UpdateDisplay (0, 0, 1, 0);
  1320. #if defined CHESSTOOL || defined XBOARD
  1321.       chesstool = 1;
  1322. #endif /* CHESSTOOL */
  1323.     }
  1324.       else if (strcmp (s, CP[129]) == 0) /* noop */ ;    /*alg*/
  1325.       else if ((strcmp (s, CP[180]) == 0) || (strcmp (s, CP[216]) == 0))    /* quit exit*/
  1326.     flag.quit = true;
  1327.       else if (strcmp (s, CP[178]) == 0)    /*post*/
  1328.     {
  1329.      /* flag.post = !flag.post;*/
  1330.     }
  1331.       else if ((strcmp (s, CP[191]) == 0) || (strcmp (s, CP[154]) == 0))    /*set edit*/
  1332.     EditBoard ();
  1333. #ifdef NONDSP
  1334.       else if (strcmp (s, CP[190]) == 0)    /*setup*/
  1335.     SetupBoard ();
  1336. #endif
  1337.       else if (strcmp (s, CP[156]) == 0)    /*first*/
  1338.     {
  1339. #if defined CHESSTOOL
  1340.       computer = white;
  1341.       opponent = black;
  1342.       flag.force = false;
  1343.       Sdepth = 0;
  1344. #endif /* CHESSTOOL */
  1345.       ok = true;
  1346.     }
  1347.       else if (strcmp (s, CP[162]) == 0)    /*go*/
  1348.     {
  1349.       ok = true;
  1350.       flag.force = false;
  1351.       if (computer == white)
  1352.         {
  1353.           computer = black;
  1354.           opponent = white;
  1355.         }
  1356.       else
  1357.         {
  1358.           computer = white;
  1359.           opponent = black;
  1360.         }
  1361.     }
  1362.       else if (strcmp (s, CP[166]) == 0)    /*help*/
  1363.     help ();
  1364.       else if (strcmp (s, CP[221]) == 0)    /*material*/
  1365.     flag.material = !flag.material;
  1366.       else if (strcmp (s, CP[157]) == 0)    /*force*/
  1367.     {flag.force = !flag.force; flag.bothsides = false;}
  1368.       else if (strcmp (s, CP[134]) == 0)    /*book*/
  1369.     Book = Book ? 0 : BOOKFAIL;
  1370.       else if (strcmp (s, CP[172]) == 0)    /*new*/
  1371.     {
  1372.       NewGame ();
  1373.       UpdateDisplay (0, 0, 1, 0);
  1374.     }
  1375.       else if (strcmp (s, CP[171]) == 0)    /*list*/
  1376.     ListGame (0xff);
  1377.       else if (strcmp (s, CP[169]) == 0 || strcmp (s, CP[217]) == 0)    /*level clock*/
  1378.        {
  1379.         GetTimeString(tstr);
  1380.     SelectLevel (tstr);
  1381.        }
  1382.       else if (strcmp (s, CP[165]) == 0)    /*hash*/
  1383.     flag.hash = !flag.hash;
  1384.       else if (strcmp (s, CP[132]) == 0)    /*beep*/
  1385.     flag.beep = !flag.beep;
  1386.       else if (strcmp (s, CP[197]) == 0)    /*time*/
  1387.     {
  1388.       SetMachineTime (sx);
  1389.     }
  1390.       else if (strcmp (s, CP[33]) == 0)    /*Awindow*/
  1391.     ChangeAlphaWindow ();
  1392.       else if (strcmp (s, CP[39]) == 0)    /*Bwindow*/
  1393.     ChangeBetaWindow ();
  1394.       else if (strcmp (s, CP[183]) == 0)    /*rcptr*/
  1395.     flag.rcptr = !flag.rcptr;
  1396.       else if (strcmp (s, CP[168]) == 0)    /*hint*/
  1397.        {
  1398.     GiveHint ();
  1399.        }
  1400.       else if (strcmp (s, CP[135]) == 0)    /*both*/
  1401.     {
  1402.       flag.bothsides = !flag.bothsides;
  1403.           if (flag.bothsides)
  1404.            {
  1405.             (void)SetTaskPri((struct Task *)myproc,0);
  1406.             thinkahead = 1;
  1407.            }
  1408.           flag.force = false;
  1409.       Sdepth = 0;
  1410.       ElapsedTime (1);
  1411.       SelectMove (opponent, 1);
  1412.       ok = true;
  1413.     }
  1414.       else if (strcmp (s, CP[185]) == 0)    /*reverse*/
  1415.     {
  1416. #ifndef AMIGA
  1417.       flag.reverse = !flag.reverse;
  1418.       ClrScreen ();
  1419.       UpdateDisplay (0, 0, 1, 0);
  1420. #endif
  1421.     }
  1422.       else if (strcmp (s, CP[195]) == 0)    /*switch*/
  1423.     {
  1424.       computer = computer ^ 1;
  1425.       opponent = opponent ^ 1;
  1426.       xwndw = (computer == white) ? WXWNDW : BXWNDW;
  1427.       flag.force = false;
  1428.       Sdepth = 0;
  1429.       if (!GameCnt)
  1430.        GetOpenings(computer);
  1431.           else
  1432.            Book = 0;
  1433.       ok = true;
  1434.     }
  1435.       else if (strcmp (s, CP[203]) == 0)    /*white*/
  1436.     {
  1437.       computer = black;
  1438.       opponent = white;
  1439.       xwndw = WXWNDW;
  1440.       flag.force = false;
  1441.       Sdepth = 0;
  1442.  
  1443.       /*
  1444.            * ok = true; don't automatically start with white command
  1445.            */
  1446.     }
  1447.       else if (strcmp (s, CP[133]) == 0)    /*black*/
  1448.     {
  1449.       computer = white;
  1450.       opponent = black;
  1451.       xwndw = BXWNDW;
  1452.       flag.force = false;
  1453.       Sdepth = 0;
  1454.  
  1455.       /*
  1456.            * ok = true; don't automatically start with black command
  1457.            */
  1458.     }
  1459.       else if (strcmp (s, CP[201]) == 0 && GameCnt > 0)    /*undo*/
  1460.     {
  1461. #ifndef AMIGA
  1462.       Undo ();
  1463. #endif
  1464.     }
  1465.       else if (strcmp (s, CP[184]) == 0 && GameCnt > 1)    /*remove*/
  1466.     {
  1467. #ifndef AMIGA
  1468.       Undo ();
  1469.       Undo ();
  1470. #endif
  1471.     }
  1472.       else if (strcmp (s, CP[160]) == 0)    /*get*/
  1473.     GetGame ();
  1474.       else if (strcmp (s, CP[207]) == 0)    /*xget*/
  1475.     GetXGame ();
  1476.       else if (strcmp (s, CP[189]) == 0)    /*save*/
  1477.     SaveGame ();
  1478.       else if (strcmp (s, CP[151]) == 0)    /*depth*/
  1479.     ChangeSearchDepth ();
  1480. #ifdef DEBUG
  1481.       else if (strcmp (s, CP[147]) == 0)    /*debuglevel*/
  1482.     ChangeDbLev ();
  1483. #endif /* DEBUG */
  1484.       else if (strcmp (s, CP[164]) == 0)    /*hashdepth*/
  1485.     ChangeHashDepth ();
  1486.       else if (strcmp (s, CP[182]) == 0)    /*random*/
  1487.     dither = DITHER;
  1488.       else if (strcmp (s, CP[152]) == 0)    /*easy*/
  1489.        {
  1490.     /*flag.easy = !flag.easy;*/
  1491.     flag.easy = flag.easy; /* mod this for menu toggle on amiga */
  1492.        }
  1493.       else if (strcmp (s, CP[143]) == 0)    /*contempt*/
  1494.     SetContempt ();
  1495.       else if (strcmp (s, CP[209]) == 0)    /*xwndw*/
  1496.     ChangeXwindow ();
  1497.       else if (strcmp (s, CP[186]) == 0)    /*rv*/
  1498.     {
  1499.       flag.rv = !flag.rv;
  1500.       UpdateDisplay (0, 0, 1, 0);
  1501.     }
  1502.       else if (strcmp (s, CP[145]) == 0)    /*coords*/
  1503.     {
  1504.       flag.coords = !flag.coords;
  1505.       UpdateDisplay (0, 0, 1, 0);
  1506.     }
  1507.       else if (strcmp (s, CP[193]) == 0)    /*stras*/
  1508.     {
  1509.       flag.stars = !flag.stars;
  1510.       UpdateDisplay (0, 0, 1, 0);
  1511.     }
  1512.       else if (strcmp (s, CP[196]) == 0)    /*test*/
  1513.     {
  1514. /*      ShowMessage (CP[108]);/*test movelist*/
  1515.           ShowMessage ("Testing..");
  1516.           func_num = 0;
  1517.       TestSpeed (MoveList, 20000);
  1518. /*      ShowMessage (CP[107]);/*test capturelist*/
  1519.           func_num++;
  1520.       TestSpeed (CaptureList, 30000);
  1521. /*      ShowMessage (CP[85]);/*test score position*/
  1522.           func_num++;
  1523.       TestPSpeed (ScorePosition, 15000);
  1524.     }
  1525.       else
  1526.       if (strcmp (s, CP[179]) == 0)    /*p*/
  1527.     ShowPostnValues ();
  1528.       else if (strcmp (s, CP[148]) == 0)    /*debug*/
  1529.     DoDebug ();
  1530.     else if (strcmp (s, "Mwpawn") == 0)        /*debug*/
  1531.         DoTable (Mwpawn);
  1532.     else if (strcmp (s, "Mbpawn") == 0)        /*debug*/
  1533.         DoTable (Mbpawn);
  1534.     else if (strcmp (s, "Mwknight") == 0)        /*debug*/
  1535.         DoTable (Mknight[white]);
  1536.     else if (strcmp (s, "Mbknight") == 0)        /*debug*/
  1537.         DoTable (Mknight[black]);
  1538.     else if (strcmp (s, "Mwbishop") == 0)        /*debug*/
  1539.         DoTable (Mbishop[white]);
  1540.     else if (strcmp (s, "Mbbishop") == 0)        /*debug*/
  1541.         DoTable (Mbishop[black]);
  1542.       else
  1543.     { /* this is where we move the humans pieces */
  1544. #if defined CHESSTOOL
  1545.       normal = (ok = VerifyMove (s, 0, &mv));
  1546. #else
  1547.       ok = VerifyMove (s, 0, &mv);
  1548. #endif
  1549. #ifdef OLDVERSION1_01
  1550.       if ((ok && mv != hint))
  1551.         {
  1552.           Sdepth = 0;
  1553.           ft = 0;
  1554.         }
  1555.       else
  1556.         Sdepth = 0;
  1557. #else
  1558. #ifndef OLD_LOOKAHEAD
  1559.           if ((ok)&&((mv != hint)||(flag.easy)||(Sdepth2<3)))
  1560.            {
  1561.             Sdepth = 0;
  1562.             need_to_zero = 1;
  1563.            }
  1564. #endif
  1565.           if (ok)
  1566.            {
  1567.         if ((!flag.easy)&&(mv == hint)&&(Sdepth2>2))
  1568.              {
  1569.               ThinkAheadDepth = Sdepth2;
  1570.               if (ThinkInARow > 4) /* if it got to 4, we would have skipped one */
  1571.                ThinkInARow = 0;
  1572.               ThinkAheadWorked = 1;
  1573.              }
  1574.             else
  1575.              { 
  1576.              ThinkInARow = 0;
  1577.               ThinkAheadDepth = 0;
  1578.               ThinkAheadWorked = 0;
  1579.              }
  1580.            } // ok
  1581. #endif
  1582.           if ((!ok))
  1583.            {
  1584.             IllegalMove = 1;
  1585.            }
  1586.           if ((ok)&&(MouseDropped))
  1587.            {
  1588.                 DoLegalMove(s);
  1589.                 MouseDropped = 0;
  1590.            }
  1591.           if (need_to_zero)
  1592.            {
  1593.             need_to_zero = 0;
  1594.             ZeroTTable();
  1595.            }
  1596.           if (!ok)
  1597.            MouseDropped = 0;
  1598.     }
  1599.     }
  1600.  
  1601.   ElapsedTime (1);
  1602.   if (flag.force)
  1603.     {
  1604.       computer = opponent;
  1605.       opponent = computer ^ 1;
  1606.     }
  1607. #if defined CHESSTOOL || defined XBOARD
  1608. #if defined CHESSTOOL
  1609.   if (normal)
  1610.     if (computer == white)
  1611.       printz ("%d. %s", ++mycnt2, s);
  1612.     else
  1613.       printz ("%d. ... %s", ++mycnt2, s);
  1614. #else
  1615.   printz ("%d. %s\n", ++mycnt2, s);
  1616. #endif
  1617. #ifdef notdef
  1618.   if (flag.post)
  1619.     {
  1620.       REG int i;
  1621.  
  1622.       printz (" %6d ", MSCORE);
  1623.       for (i = 1; MV[i] > 0; i++)
  1624.     {
  1625.       algbr ((INTSIZE) (MV[i] >> 8), (INTSIZE) (MV[i] & 0xFF), false);
  1626.       printz ("%5s ", mvstr[0]);
  1627.     }
  1628.     }
  1629.   printz ("\n");
  1630. #endif
  1631. #endif /* CHESSTOOL */
  1632.   signal (SIGINT, TerminateSearch);
  1633. #ifndef MSDOS
  1634.   signal (SIGQUIT, TerminateSearch);
  1635. #endif /* MSDOS */
  1636. }
  1637.  
  1638.  
  1639. void
  1640. ElapsedTime (INTSIZE int iop)
  1641.  
  1642.  
  1643. /*
  1644.  * Determine the time that has passed since the search was started. If the
  1645.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1646.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1647.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1648.  */
  1649.  
  1650. {
  1651. #ifndef MSDOS
  1652.   extern int errno;
  1653. #ifdef AMIGA
  1654.    struct IntuiMessage *localmessage;
  1655.    long __aligned class,code;
  1656. #endif
  1657. #ifndef AMIGA
  1658. #ifdef FIONREAD
  1659.   if (i = ioctl ((int) 0, FIONREAD, &nchar))
  1660.     {
  1661.       perror ("FIONREAD");
  1662.       fprintf (stderr,
  1663.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  1664.     i, errno, FIONREAD);
  1665.       exit (1);
  1666.     }
  1667.  
  1668.   if (nchar)
  1669.     {
  1670.       if (!flag.timeout)
  1671.     flag.back = true;
  1672.       flag.bothsides = false;
  1673.     }
  1674. #endif /*FIONREAD*/
  1675. #endif
  1676. #ifdef AMIGA /* check if I need to interrupt thinking */
  1677.   if (thinkahead)
  1678.   {
  1679.   while ( (localmessage = (struct IntuiMessage *)
  1680.     GetMsg(wG->UserPort) )) /* got a message at window port */
  1681.    {
  1682.         if (localmessage->Class == RAWKEY)
  1683.          {
  1684.           if (localmessage->Code < 56)
  1685.            code = cookedchar[localmessage->Code];
  1686.           else
  1687.            code = 0;
  1688.          }
  1689.         else
  1690.          {
  1691.           code = 'A';
  1692.          }
  1693.         if (isalpha(code))
  1694.          {
  1695.           (void)SetTaskPri((struct Task *)myproc,procpri);
  1696.           if (!TCflag)
  1697.            backsrchaborted = 1;
  1698.           if (!flag.timeout)
  1699.            {
  1700.             flag.back = true;
  1701.            }
  1702.           flag.bothsides = false;
  1703.       globalmessage_valid = 0xffff;
  1704.           globalmessage = *localmessage;
  1705.          }
  1706.     ReplyMsg((struct Message *)localmessage);
  1707.    }
  1708.   }
  1709.   else if (thinking2) /* check for move now menu item */
  1710.   {
  1711.   while ( (localmessage = (struct IntuiMessage *)
  1712.     GetMsg(wG->UserPort) )) /* got a message at window port */
  1713.    {
  1714.     class = localmessage->Class;
  1715.         code = localmessage->Code;
  1716.     ReplyMsg((struct Message *)localmessage);
  1717.         if ((class == MENUPICK))
  1718.           {
  1719.         if (ItemAddress(&Menu1,code) == &MenuItem6)
  1720.             {
  1721.              if (!flag.timeout)
  1722.               {
  1723.                flag.back = true;
  1724.               flag.musttimeout = true;
  1725.               }
  1726.              flag.bothsides = false;
  1727.             }
  1728.           }
  1729.    }
  1730.   }
  1731. #endif
  1732. #else
  1733.   if (kbhit ())
  1734.     {
  1735.       if (!flag.timeout)
  1736.     flag.back = true;
  1737.       flag.bothsides = false;
  1738.     }
  1739. #endif /* MSDOS */
  1740.   et = (time ((long *) 0) - time0) * 100;
  1741.   ETnodes = NodeCnt + ZNODES;
  1742.   if (et < 0)
  1743.     et = 0;
  1744.   if (iop == 1)
  1745.     {
  1746.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1747.     flag.timeout = true;
  1748.       ETnodes = NodeCnt + ZNODES;
  1749.       time0 = time ((long *) 0);
  1750.     }
  1751. #ifdef AMIGA
  1752.     UpdateClocks ();
  1753. //sprintf(tstr,"%d",et);
  1754. //ShowMessage(tstr);
  1755. #endif
  1756. }
  1757.  
  1758.  
  1759. void
  1760. SetTimeControl (void)
  1761. {
  1762.  int tmp;
  1763.   if (TCflag)
  1764.     {
  1765.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  1766.       TimeControl.clock[white] = 6000L * TCminutes + TCseconds * 100;
  1767.       TimeControl.clock[black] = 6000L * TCminutes + TCseconds * 100;
  1768.       tmp = (TCminutes*60+TCseconds)/TCmoves;
  1769.       if (tmp < 10)
  1770.        {
  1771.         GlobalTgtDepth = 2;
  1772.        }
  1773.       else if (tmp < 180)
  1774.        {
  1775.         GlobalTgtDepth = 3;
  1776.        }
  1777.       else
  1778.        GlobalTgtDepth = 4;
  1779.     }
  1780.   else
  1781.     {
  1782.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  1783.       TimeControl.clock[white] = TimeControl.clock[black] = 0;
  1784.     }
  1785.   flag.onemove = (TCmoves == 1);
  1786.   et = 0;
  1787.   ElapsedTime (1);
  1788. }
  1789.